"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 硬件故障类--CPU
Case Name   : 备节点重建时，备节点CPU占用率高
Create At   : 2021/07/08
@zou_jialiang0505328126
Description :
    1. 备节点注入CPU占用率高故障
        ./cfe "inject rCPU_Overloadau (cpuid1, cpuid2, usage, time)
        values(1, 7, 85, 40)"
    2. 备节点重建
    gs_ctl build -D xxx
Expect      :
    1.成功
    2.成功
History     :
    Modified by wx1115623 2023/2/6:修改注入CPU占用率故障断言信息
    Modified by wx1115623 2023/2/20:修改CPU占用率故障注入语句，规避故障注入失败
"""

import os
import time
import unittest
from yat.test import Node
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH
from testcase.utils.CommonSH import RestartDbCluster
from testcase.utils.Logger import Logger


class CPUOverloadAndRunTPCC(unittest.TestCase):
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.sta_root_node = Node(node='Standby1Root')
        self.com = Common()
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.sta_sh = CommonSH('Standby1DbUser')
        self.constant = Constant()

    def test_main(self):
        self.log.info("--------------1.备节点注入CPU占用率高故障-------------")
        shell_cmd = "cat /proc/cpuinfo |grep 'processor'|wc -l"
        self.log.info(shell_cmd)
        cpu_num = self.sta_root_node.sh(shell_cmd).result()
        self.log.info(cpu_num)
        inject_command = f"rCPU_Overloadau (cpuid1, cpuid2, usage, time) " \
            f"values(0, {int(cpu_num)-1}, 85, 40)"
        result = self.com.cfe_inject(self.sta_root_node, inject_command)
        self.log.info(result)
        flag = self.constant.cfe_inject_rcpu_overloadau_success_msg in result
        if not flag:
            res = self.com.cfe_inject(self.sta_root_node, inject_command)
            self.log.info(res)
            self.assertIn(self.constant.cfe_inject_rcpu_overloadau_success_msg,
                          res.strip())

        self.log.info("-------------2. 备节点重建---------------")
        result = self.sta_sh.executeGsctl('build',
                                          self.constant.REBUILD_SUCCESS_MSG)
        self.assertTrue(result)
        db_status = self.primary_sh.getDbClusterStatus("status")
        self.assertTrue(db_status)

    def tearDown(self):
        time.sleep(40)
        self.log.info(f"-----{os.path.basename(__file__)} end-----")
